// const { default: axios } = require("axios");
require('../css/register.css');

window.onload = function () {
    let register = document.querySelector('.register')
    register.addEventListener('click', function () {
        location.href = './login.html'
    })

    let str = ''
    let captcha1 = new CaptchaMini();
    captcha1.draw(document.querySelector('#captcha1'), r => {
        // console.log(r, '验证码1');
        str = r
    });

    let btn = document.querySelector('.btn')
    let user1 = document.querySelector('.user1 input')
    let codebigBox = document.querySelector('.codebigBox input')
    let user = document.querySelector('.user input')
    let password = document.querySelector('.password input')
    btn.addEventListener('click', function () {
        console.log(user1, codebigBox, user, password);
        if (!(user1.value && codebigBox.value && user.value && password.value)) {
            // alert('输入框不能为空')
            common.toast('输入框不能为空', false);

            return
        };
        let phone = /^1[3-9]\d{9}$/
        if (!phone.test(user1.value)) {
            // alert('手机号输入错误')
            common.toast('手机号输入错误', false);

            return
        };
        if (codebigBox.value != str.toLocaleLowerCase()) {
            // alert('验证码错误')
            common.toast('验证码错误', false);

            return
        };
        let paw = /^\w{6,12}$/
        if (!paw.test(password.value)) {
            // alert('密码错误')
            common.toast('密码错误', false);

            return
        };
        if (!paw.test(user.value)) {
            // alert('密码错误')
            common.toast('密码错误', false);

            return
        };
        if (user.value != password.value) {
            // alert('两次密码不匹配')
            common.toast('两次密码不匹配', false);

            return
        }
        // 接口
        axios.post('/api/user/register', {
            account: user1.value,
            password: user.value,
        }).then(function (res) {
            if (res.data.errno == 0) {
                // alert('注册成功');
                common.toast('注册成功', false);

                location.href = './login.html'
            } else {
                alert(res.data.message)
            }
        })
    })


}

